package service

import (
	"core/db"
	"core/entity"
)

func ListProjects(pageNum int, pageSize int, projectGroupId string) (projects []entity.Project, err error) {
	projects = make([]entity.Project, 0)

	err = db.DB.Raw(`select
jp.id, jp.dsl_source , jp.dsl_type , jp.workflow_name , jp.workflow_version, jp.workflow_ref , jp.workflow_description, jp.enabled, jp.mutable
from project_link_group plg
left join jianmu_project jp on plg.project_id =jp.id
left join project_group pg on plg.project_group_id =pg.id
where project_group_id = ?`, projectGroupId).Scan(&projects).Error

	if err != nil {
		return nil, err
	}

	return projects, nil
}

func FindProjectById(projectId string) (project *entity.Project, err error) {
	project = new(entity.Project)
	err = db.DB.Where("id = ?", projectId).Find(project).Error
	if err != nil {
		return nil, err
	}
	return project, nil
}

func TriggerProject(projectId string) (err error) {
	Queue.TriggerQueue <- projectId
	return
}
